package com.daviteq.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.daviteq.dto.ResponeUser;
import com.daviteq.dto.RoleUser;
import com.daviteq.ems.entity.UserEntity;
import com.daviteq.user.service.UserService;
import com.google.gson.Gson;






/**
 * Servlet implementation class UserController
 */

public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		String action = request.getParameter("action");
		Gson gs = new Gson();
		if ("getAllUser".equals(action)) {
			UserService userService = new UserService();
			List<UserEntity> listAllUser=userService.getAll();
			out.println(gs.toJson(listAllUser));
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter printer=response.getWriter();
		Gson gs=new Gson();
		boolean result=false;
		String action=request.getParameter("action");
		UserService userService=new UserService();
		if("addUser".equals(action)){
			Date todayD=new Date(System.currentTimeMillis()); 
			SimpleDateFormat dayFormat= new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
			String todayS=dayFormat.format(todayD.getTime()); 
			String userJson = request.getParameter("user");
			UserEntity user = gs.fromJson(userJson, UserEntity.class);
			user.setLastLogin(todayS);
			user.setCreatedDate(todayS);
			user.setLastUpdated(todayS);
			result=userService.insertUser(user);
			
		}else if("deleteUser".equals(action)){
			String userDelete=request.getParameter("deleteID");
			if(userDelete.equals("")==false){
				result=userService.deleteUser(userDelete);
			}
		}else if("editUser".equals(action)){
			String userEdit=request.getParameter("editID");
			if(userEdit.equals("")==false){
				UserEntity getUser=userService.getUserById(userEdit);
				//---------innit group user
				List<RoleUser> groupUser = new ArrayList<RoleUser>();
				for(int i=0;i<3;i++){
					RoleUser group=new RoleUser();
					group.setGroup("Group "+i);
					groupUser.add(group);
				}
				//------------------------------
				ResponeUser respone=new ResponeUser();
				respone.setGroupUser(groupUser);
				respone.setUser(getUser);
				//System.out.println(gs.toJson(respone));
				printer.println(gs.toJson(getUser));
				return ;
			}
		}else if("updateUser".equals(action)){
			String userJson = request.getParameter("user");
			UserEntity user = gs.fromJson(userJson, UserEntity.class);
			String userRole = request.getParameter("roleUser");
			if(!userRole.equals("")){
//				System.out.println(userRole);
				String[] roles=userRole.split(",");
				for(String role : roles){
					System.out.println(role);
				}
			}
			result=userService.updateUser(user);
		}
		printer.print(gs.toJson(result));
	}
	
}
